<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>测试打乱试卷</title>
  </head>
  <body>
    <script>
      const shuffle = (arr) => {
        const res = [];
        while (arr.length > 0) {
          const index = Math.floor(Math.random() * arr.length);
          const item = arr[index];
          if(item.results && item.results.length > 0) {
            item.results = shuffle(item.results);
          }
          res.push(item);
          arr.splice(index, 1);
        }
        return res;
      };

      const questions = [
        {
          id: 111,
          title: "关于将JDK配置到Path环境变量，下列描述正确的是（  ）",
          type: "单选题",
          correct: ["C"],
          styleIndex: 0,
          results: [
            {
              option: "A",
              value: "为了能在任意目录下，都能访问 .java 文件",
            },
            {
              option: "B",
              value: "为了能在任意目录下，都能访问 .class文件",
            },
            {
              option: "C",
              value: "为了能在任意目录下，都能访问 jdk 中 bin 目录的工具",
            },
            {
              option: "D",
              value: "以上描述全部错误",
            },
          ],
        },
        {
          id: 222,
          title: "以下选项中，那些属于JDK工具？",
          type: "多选题",
          correct: ["A", "B", "C", "D"],
          styleIndex: 0,
          results: [
            {
              option: "A",
              value: "java编译器",
            },
            {
              option: "B",
              value: "java运行工具",
            },
            {
              option: "C",
              value: "java文档生成器",
            },
            {
              option: "D",
              value: "java打包工具",
            },
          ],
        },
        {
          id: 333,
          title: "当运行—个Java程序时，传递参数的格式是哪项?",
          type: "单选题",
          correct: ["C"],
          styleIndex: 0,
          results: [
            {
              option: "A",
              value: "java类名 参数1,参数2",
            },
            {
              option: "B",
              value: "javac类名 参数1 参数2",
            },
            {
              option: "C",
              value: "java类名 参数1 参数2",
            },
            {
              option: "D",
              value: "java类名 参数1+参数2",
            },
          ],
        },
      ];
      const luan = shuffle(questions);
      console.log(luan);
    </script>
  </body>
</html>
